package com.qdxht.dispenser.ui.activity.test;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.widget.TextView;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivitySignalTestBinding;

import java.util.List;

public class SignalTestActivity extends BaseActivity<ActivitySignalTestBinding> {

    private TelephonyManager telephonyManager;
    private PhoneStateListener phoneStateListener;

    @Override
    protected ActivitySignalTestBinding getBinding() {
        return ActivitySignalTestBinding.inflate(getLayoutInflater());
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SignalTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        requestPermission();
    }

    @Override
    protected void initData() {
    }

    private void requestPermission() {
        XXPermissions.with(this)
                .permission(Manifest.permission.READ_PHONE_STATE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            initSignalStrengthMonitor();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.show("请授予电话权限以监测信号强度");
                            XXPermissions.startPermissionActivity(mActivity, permissions);
                        } else {
                            ToastUtils.show("获取权限失败");
                        }
                    }
                });
    }

    private void initSignalStrengthMonitor() {
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                super.onSignalStrengthsChanged(signalStrength);
                updateSignalInfo(signalStrength);
            }
        };

        // 注册监听器
        if (telephonyManager != null) {
            telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
            updateNetworkType();
        }
    }

    private void updateSignalInfo(SignalStrength signalStrength) {
        if (signalStrength == null) return;

        int gsmSignalStrength = -1;
        int level = -1;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            gsmSignalStrength = signalStrength.getCellSignalStrengths().get(0).getDbm();
            level = signalStrength.getLevel(); // 0-4
        } else {
            if (signalStrength.isGsm()) {
                gsmSignalStrength = signalStrength.getGsmSignalStrength() * 2 - 113;
                level = getSignalLevel(gsmSignalStrength);
            }
        }

        binding.tvSignalStrength.setText("信号强度: " + gsmSignalStrength + " dBm");
        binding.tvSignalLevel.setText("信号等级: " + level + "/4");
    }

    private void updateNetworkType() {
        String networkType = "未知";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            switch (telephonyManager.getDataNetworkType()) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    networkType = "2G";
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                    networkType = "3G";
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:
                    networkType = "4G";
                    break;
                case TelephonyManager.NETWORK_TYPE_NR:
                    networkType = "5G";
                    break;
            }
        }
        binding.tvNetworkType.setText("网络类型: " + networkType);
    }

    private int getSignalLevel(int dbm) {
        if (dbm >= -75) return 4;
        if (dbm >= -85) return 3;
        if (dbm >= -95) return 2;
        if (dbm >= -100) return 1;
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (telephonyManager != null && phoneStateListener != null) {
            telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
        }
    }
} 